﻿using RantopadService.Business;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services.Description;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RantopadService.query
{
    public partial class QueryKeySetting : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                initTypeList();
                BindData();
            }
            
           
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            BindData();
        }

        private void BindData()
        {
            var data = KeySettingBusiness.getAllKeySettings("","");
            GridView1.DataSource = data;
            GridView1.DataBind();
            LabelMsg.Text = string.Format("共{0}条数据", data.Rows.Count);
        }
        private void BindData(string wherelike)
        {
            var data = KeySettingBusiness.getAllKeySettings(wherelike, keySettingType.SelectedValue);
            GridView1.DataSource = data;
            GridView1.DataBind();
            LabelMsg.Text = string.Format("共{0}条数据",data.Rows.Count);
           
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {

        }

        protected void Search_Click(object sender, EventArgs e)
        {
            BindData(TextWhereLike.Text.ToString());
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            //GridView1.EditIndex = -1;
            //GridView1.DataBind();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            //GridView1.EditIndex = e.NewEditIndex;
            //GridView1.DataBind();
            
            //var name = GridView1.DataKeys[e.].Value.ToString();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id =GridView1.DataKeys[e.RowIndex].Value.ToString();

            if (KeySettingBusiness.delKeySettingByID(id) > 0)
                Response.Write("<script>alert('删除成功！')</script>");
            else
                Response.Write("<script>alert('删除失败！')</script>");
            BindData();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow) 
            { 
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) 
            { 
            ((LinkButton)e.Row.Cells[5].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除：\"" + e.Row.Cells[0].Text + "\"吗?')"); 
            } 
            } 
        }

        protected void ButAddOrUpdate_Click(object sender, EventArgs e)
        {
            if(chechUser())
            {
                int success = 0;

                if (ButAddOrUpdate.Text == "添加") success = KeySettingBusiness.addKeySetting(Name.Text, Content.Text, DropDownList2.SelectedValue.ToString());

                if (ButAddOrUpdate.Text == "修改") success = KeySettingBusiness.updateKeySetting(DropDownList2.SelectedValue.ToString(), Name.Text, Content.Text, ID.Text);
                
                if(success>0)
                {
                    BindData();
                    Msg.Text = ButAddOrUpdate.Text + "成功";
                }
                else
                {
                    Msg.Text = ButAddOrUpdate.Text + "失败";
                }

            }
        }
        void initTypeList()
        {
            var data = KeySettingBusiness.getAllKeySettingsType("");
            keySettingType.DataTextField = "KEY_SETTING_TYPE_NAME";
            keySettingType.DataValueField = "ID";
            DropDownList2.DataTextField = "KEY_SETTING_TYPE_NAME";
            DropDownList2.DataValueField = "ID";
            keySettingType.DataSource = data;
            DropDownList2.DataSource = data;
            keySettingType.DataBind();
            DropDownList2.DataBind();
           
        }
        private bool chechUser()
        {
            Msg.Text = "";
            if(Content.Text == ""||Name.Text == "")
            {
                Msg.Text = "信息不全";
                return false;
            }
            
            return true;
        }

        protected void ButClear_Click(object sender, EventArgs e)
        {
            Content.Text = "";
            Name.Text = "";
            
            ButAddOrUpdate.Text = "添加";
            

        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EditUser")
            {
                // Convert the row index stored in the CommandArgument
                // property to an Integer.
                int index = Convert.ToInt32(e.CommandArgument);

                // Get the last name of the selected author from the appropriate
                // cell in the GridView control.
                GridViewRow selectedRow = GridView1.Rows[index];
                //TableCell contactName = selectedRow.Cells[1];
                //string contact = contactName.Text;
                //Msg.Text = contact;

                Content.Text = selectedRow.Cells[2].Text;
                Name.Text = selectedRow.Cells[1].Text;
                ID.Text = selectedRow.Cells[0].Text;
                ButAddOrUpdate.Text = "修改";
                DropDownList2.Text = KeySettingBusiness.getKeySettingsTypeID( selectedRow.Cells[3].Text);
                //DropDownList2.SelectedValue = selectedRow.Cells[3].Text;
                ID.Enabled = false;
                

            }
        } 
    }
}